SELECT
 count(*) count
FROM
(
SELECT
p.id,
p.good_name goodsName,
p.img_url,
IFNULL(sku.sku_costprice,0) costprice,
IFNULL(sku.sku_saleprice,0) saleprice,
IFNULL(sku.sku_sales,0)  sales,
IFNULL(moc.commentCount,0)  commentCount

FROM
	jd_goods_spu p
LEFT JOIN (
	SELECT
		k.goods_id , k.sku_costprice,MIN(k.sku_saleprice) sku_saleprice,sum(k.sku_sales) sku_sales,MAX(k.sku_stock) sku_stock
	FROM
		jd_goods_sku k
	GROUP BY k.goods_id
) sku  on sku.goods_id = p.id
left join (select count(goods_id) commentCount,goods_id from jd_order_comment  GROUP BY goods_id) moc  on moc.goods_id = p.id

WHERE 1=1 and p.state = '1' and sku.sku_stock> 0
<#if flag?? && flag == '0'>
	AND p.hot_flag = '1'
</#if>
<#if keyWord?? && keyWord != ''>
	and p.good_name like CONVERT('%${keyWord}%' USING utf8) COLLATE utf8_unicode_ci
</#if>
<#if classifyId?? && classifyId != ''>
	and p.classify_id like '%${classifyId}%'
</#if>
<#if lowPrice?? && lowPrice != '' && highPrice?? && highPrice != ''>
	and sku.sku_saleprice>='${lowPrice}'
	and sku.sku_saleprice<='${highPrice}'
</#if>
<#if lowPrice?? && lowPrice == '' && highPrice?? && highPrice != ''>
	and sku.sku_saleprice<='${highPrice}'
</#if>
<#if lowPrice?? && lowPrice != '' && highPrice?? && highPrice == ''>
	and sku.sku_saleprice>='${lowPrice}'
</#if>
<#if flag?? && flag == '2'>
	ORDER BY p.pubilsh_time DESC , good_name DESC,sales DESC
</#if>
)a
